import os
from dotenv import load_dotenv
import requests
from Model_foundation.prompt_test import prompt

"""
字节模型对话模型： provider_bytedance

curl https://ark.cn-beijing.volces.com/api/v3/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ARK_API_KEY" \
  -d '{
    "model": "ep-20250226225639-lbdsg",
    "messages": [
      {"role": "system","content": "你是人工智能助手."},
      {"role": "user","content": "常见的十字花科植物有哪些？"}
    ]
  }'

"""
# 加载 .env 文件中的环境变量
dotenv_path = os.path.join(os.path.dirname(__file__), '../.env')
load_dotenv(dotenv_path=dotenv_path)

api_key = os.getenv("DASHSCOPE_API_KEY")
# print(api_key)
if not api_key:
    raise ValueError("DASHSCOPE_API_KEY is not set in the environment variables.")

def call_bytedance_chat(prompt, system=None, format="text", model="ep-20250226225639-lbdsg"):
    payload = {
        "model": model,
        "messages": [],
        "stream": False,
        "max_tokens": MAX_TOKENS,
        "stop": ["null"],
        "temperature": 0.7,
        "top_p": 0.7,
        "top_k": 50,
        "frequency_penalty": 0.5,
        "n": 1,
    }

    if system is not None:
        payload["messages"].append({
            "role": "system",
            "content": system
        })
    payload["messages"].append({
        "role": "user",
        "content": prompt
    })

#     调大模型请求
    try:
        response = requests.request("POST", URL, json=payload, headers=headers)
        return response.json()["choices"][0]["message"]["content"]
    except Exception as e:
        print(e)
        return None

if __name__ == '__main__':
    URL = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
    headers = {
        "Authorization": f"Bearer {os.environ['DASHSCOPE_API_KEY']}",
        "Content-Type": "application/json"
    }
    prompt = "今天是几号,output JSON"
    system = "You are a helpful assistant designed to output JSON."
    MAX_TOKENS = 4096
    call_bytedance_chat(prompt,system,)